"""
animation.py

implementation of animations, uses sprite sheets."""

import pygame

import data

class Animation(pygame.sprite.Sprite):
    def __init__(self, image, pos, delay_ms):
        "give the filename, x,y coords and delay between transitions."
        pygame.sprite.Sprite.__init__(self)

        s = data.Spritesheet(image)
        self.images = s.load_strip((0, 0, 30, 30), 3, colorkey=-1)

        self.image = self.images.pop(0)
        self.rect = self.image.get_rect(topleft=pos)

        self._delay = delay_ms
        # the elapsed time since last change.
        self._elapsed = 0

    def update(self, kwargs):
        'update images according to the time elapsed.'
        time = kwargs.get('time')
        if time:
            self._elapsed += time
            if self._elapsed > self._delay:
                self._elapsed = 0 # reset elapsed time.
                try:
                    self.image = self.images.pop(0)
                except IndexError:
                    self.kill()

